package com.yl.auth.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.yl.auth.model.entity.Tijian;
import com.yl.auth.service.TijianService;
import com.yl.common.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author L
 * @since 2023-07-24
 */
@RestController
@RequestMapping("/admin/auth/tijian")
@Api(tags = "体检报告")
@CrossOrigin
public class TijianController {


    @Resource
    private TijianService tijianService;

    @PostMapping("/getZiLiao/{id}")
    @ApiOperation(value = "上传资料")
    public Result getZiLiao(@RequestBody Tijian tijian, @PathVariable Long id) {
        if(tijian.getId()!=null) {
            tijianService.updateById(tijian);
        }else {
            tijianService.save(tijian);
        }
        return Result.ok();
    }

    @GetMapping("/getTijianBaoGao/{id}")
    @ApiOperation(value = "回显体检报告信息")
    public Result getTijianBaoGao(@PathVariable Integer id) {
//        QueryWrapper<Tijian> queryWrapper=new QueryWrapper<>();
//        queryWrapper.eq("laoren_id",id);
//        int count = tijianService.count(queryWrapper);
//        if(count>=1){
//            LambdaQueryChainWrapper<Tijian> select = tijianService.lambdaQuery().eq(Tijian::getLaorenId, id).select();
//            Tijian one = tijianService.getOne(select);
//            return Result.ok(one);
//        }else {
//            return Result.ok();
//        }
        LambdaQueryWrapper<Tijian> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Tijian::getLaorenId,id);
        Tijian one = tijianService.getOne(wrapper);
        return Result.ok(one);
    }

    /*图片上传上传图片接口*/
    /*图片上传地址*/
    //String fileSavePath = "D:/IDEABoot课程/ylHomes/service-ylHouse/src/main/resources/templates/upload/";
    @Value("${web.upload-path}")
    private String fileSavePath;
    //String fileSavePath = "D\\:/BaiduNetdiskDownload/jiushuifile/";
    @PostMapping("/saveUpload")
    @ApiOperation(value = "图片上传")
    @CrossOrigin
    public Result uploadFile (@ApiParam(name = "file", value = "上传", required = true) @RequestPart("file") List<MultipartFile> file, HttpServletRequest request) throws IOException {
        /*格式化时间*/
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd/");
        /*将时间转化成字符串*/
        Date date = new Date();
        String directory = simpleDateFormat.format(date);
            List<String> a = new ArrayList<String>();
            for (MultipartFile multipartFile : file) {
                /*获取图片的名字*/
                String imageName = multipartFile.getOriginalFilename();
                /*文件路径是否存在
                 * 如果不存在,则创建
                 */
                File dir = new File(fileSavePath +"/upload/" + directory);
                if (!dir.exists()) {
                    dir.mkdirs();
                }
                System.out.println("图片上传位置" + fileSavePath + directory);
                /*给文件创建一个新的名字*/
                String suffix = multipartFile.getOriginalFilename().substring(multipartFile.getOriginalFilename().lastIndexOf("."));
                /*图片新的名字*/
                String newFileName = UUID.randomUUID().toString().replaceAll("-", "") + suffix;
                System.out.println("上传的图片位置" + fileSavePath + directory + newFileName);
                /*创建新文件*/
                File newFile = new File(fileSavePath +"/upload/" + directory + newFileName);
                multipartFile.transferTo(newFile);
                String url = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + "/images/" + directory + newFileName;
                System.out.println("图片上传成功的url:" + url);
                String sql = "/upload/" + directory + newFileName;
                a.add(sql);
                /*返回给前端的字符串地址,然后添加的时候将这一串字符串加入到数据库中*/
//            return Result.ok(sql);
                System.out.println(sql);
            }
            //将集合所有的数据末尾加逗号拼接成字符串存入数据库中
            String result = a.stream().collect(Collectors.joining(","));
            System.out.println(result);
            return Result.ok(result);
    }



}

